/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.view;

import org.chaoticengine.ast.GlobalData;
import org.chaoticengine.ast.model.AirShip;
import org.chaoticengine.ast.pattern.ViewController;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class AirShipMapView extends ViewController<AirShip> {
    protected AirShip ship = null;
    protected int tileW = 0;
    protected int tileH = 0;
    protected Color color = Color.lightGray;
    protected Image img = null;
    protected Image imgSelected = null;
    protected boolean hover = false;

    public AirShipMapView() {
        TileFactory tf = TileFactory.getInstance();

        tileW = tf.getTileWidth();
        tileH = tf.getTileHeight();

        img = tf.getTile("Airship").getImage();
        imgSelected = tf.getTile("Airship-selected").getImage();
        this.setBoundingRectangle(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
    }

    @Override
    public AirShip getModel() {
        return(ship);
    }

    @Override
    public void keyPressed(int key, char c) {
    }

    @Override
    public void keyReleased(int key, char c) {
    }

    @Override
    public void onMouseClick(int x, int y, int button) {
    }

    @Override
    public void onMouseEnter() {
        hover = true;
    }

    @Override
    public void onMouseExit() {
        hover = false;
    }

    @Override
    public void onMouseMove(int oldx, int oldy, int newx, int newy) {
    }

    @Override
    public void onMouseRelease(int x, int y, int button) {
        System.out.println("MOUSE RELEASE");

        if (button == Input.MOUSE_LEFT_BUTTON) {
            GlobalData.getInstance().setSelected(this);
        }
    }

    @Override
    public void onMouseWheelDown() {
    }

    @Override
    public void onMouseWheelUp() {
    }

    @Override
    public void refresh(AirShip model) {
        this.ship = model;

        Vector2f p = ship.getPosition();
        this.setPosition(new Vector2f(p.x * tileW, p.y * tileH));
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        // Draw the ship
        if ((selected) || (hover)) {
            grphcs.drawImage(imgSelected,
                    this.getPosition().x,
                    this.getPosition().y);
        } else {
            grphcs.drawImage(img,
                    this.getPosition().x,
                    this.getPosition().y);
        }
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) {
    }

    public int getTileWidth() {
        return(tileW);
    }

    public void setTileWidth(int width) {
        this.tileW = width;
        this.setBoundingRectangle(new Rectangle(0, 0, tileW, tileH));
    }

    public int getTileHeight() {
        return(tileH);
    }

    public void setTileHeight(int height) {
        this.tileH = height;
        this.setBoundingRectangle(new Rectangle(0, 0, tileW, tileH));
    }

    @Override
    public void onDeselect() {
        color = Color.lightGray;
    }

    @Override
    public void onSelect() {
        color = Color.lightGray.brighter();
    }
}
